<template>
  <div id="rental-car-orders" class="row">
    <div class="card col-12">
      <div class="card-header my-0 py-0">
        <nav aria-label="breadcrumb" role="navigation">
          <ol class="breadcrumb pb-0">
            <li class="breadcrumb-item">
              <router-link to="/car/orders">租车订单</router-link>
            </li>
            <li class="breadcrumb-item active" aria-current="page">列表</li>
          </ol>
        </nav>
      </div>
    </div>
    <div class="card col-12 d-none d-md-block">
      <form>
        <div class="row mb-2">
          <div class="col-1">
            <select class="form-select" v-model.number="status">
              <option value="-1">所有订单</option>
              <option value="1">待处理</option>
              <option value="2">处理中</option>
              <option value="4">已取消</option>
              <option value="8">已完成</option>
            </select>
          </div>
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              size="10"
              placeholder="订单号"
              v-model.trim="orderNo"
            />
          </div>
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              size="10"
              placeholder="联系人"
              v-model.trim="linkman"
            />
          </div>
          <div class="col-1">
            <input
              type="textfield"
              class="form-control"
              size="10"
              placeholder="手机号"
              v-model.trim="mobile"
            />
          </div>
          <div class="col-1">
            <my-date-picker
              id="ddate"
              v-model="ddate"
              class="form-control"
              placeholder="用车日期"
            ></my-date-picker>
          </div>
          <div class="col">
            <button
              type="button"
              class="btn btn-primary"
              @click.stop="search()"
            >
              查找
            </button>
            <button
              type="button"
              class="btn btn-secondary ms-2"
              @click.stop="reset()"
            >
              重置
            </button>

            <router-link to="/car/order" class="btn btn-success float-end"
              >新建</router-link
            >
          </div>
        </div>
      </form>
    </div>

    <div class="card col-12 mt-2">
      <table class="table table-striped table-sm table-hover">
        <thead class="table-light">
          <tr>
            <th class="d-none d-md-table-cell">订单号</th>
            <th class="d-none d-md-table-cell">类型</th>
            <th>起点</th>
            <th class="d-none d-md-table-cell">终点</th>
            <th>用车时间</th>
            <th>状态</th>
            <th></th>
          </tr>
        </thead>
        <tbody>
          <tr
            v-for="info in orders"
            @click="showDetail(info)"
            :key="`rental_car_order_` + info.id"
          >
            <td class="d-none d-md-table-cell">{{ info.orderNo }}</td>
            <td class="d-none d-md-table-cell">
              {{ showUseTypeDesc(info.useType) }}
            </td>
            <td>
              <span>{{ info.departureAddress }}</span>
            </td>
            <td class="d-none d-md-table-cell">
              <span>{{ info.arrivalAddress }}</span>
            </td>
            <td>{{ info.useTime }}</td>
            <td>{{ showStatusDesc(info.status) }}</td>
            <td>
              <i
                class="fa fa-angle-right text-warningfa-2"
                aria-hidden="true"
              ></i>
            </td>
          </tr>
        </tbody>
      </table>
      <div class="card-body">
        <my-pagination
          :row-count="sc.rowCount"
          :page-total="sc.pageTotal"
          :page-no="sc.pageNo"
          @next-page="nextPage"
          @prev-page="prevPage"
          @direct-page="directPage"
        ></my-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import MyPagination from "@/components/my-pagination.vue";
import MyDatePicker from "@/components/my-datepicker.vue";

import {
  searchRentalCarOrders,
  showOrderStatusDesc,
  showUseTypeDesc,
} from "@/api/rentalcar.js";
import $ from "jquery";

export default {
  components: {
    MyPagination,
    MyDatePicker,
  },
  data() {
    return {
      detailShowing: false,
      orders: [],
      sc: {
        pageNo: 1,
        pageSize: 10,
        rowCount: 0,
        pageTotal: 0,
      },
      status: -1,
      orderNo: "",
      mobile: "",
      ddate: "",
      linkman: "",
    };
  },
  mounted: function () {
    this.search();
  },
  methods: {
    back: function () {
      this.$router.go(-1);
    },
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", { loading: false });
    },
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    changeBgByStatus: function (orderStatus) {
      switch (orderStatus) {
        case 0:
          return "text-muted";
        case 1:
          return "bg-success text-white";
        case 4:
          return "";
      }
    },
    search: function () {
      const params = {
        pageNo: this.sc.pageNo,
        pageSize: this.sc.pageSize,
        status: this.status,
        orderNo: this.orderNo,
        mobile: this.mobile,
        useDate: this.ddate,
        linkman: this.linkman,
      };

      this.showLoading();

      searchRentalCarOrders(
        params,
        (v) => {
          this.orders = v.dataList;
          this.sc = v.page;
        },
        () => this.hideLoading()
      );
    },
    reset: function () {
      this.status = -1;
      this.orderNo = "";
      this.mobile = "";
      this.ddate = "";
      this.linkman = "";
    },
    showDetail: function (orderInfo) {
      this.$router.push("/car/order/" + orderInfo.id);
    },
    showStatusDesc: function (status) {
      return showOrderStatusDesc(status);
    },
    showUseTypeDesc: function (status) {
      return showUseTypeDesc(status);
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
